import HTTP_CODE from '../constant/http-code'
import * as request from '../constant/fetch-request'
import {message} from 'antd'
import {convertContent} from '../constant/lang-util'

export const refreshExcellentQuizComments = excellentQuizComments => {
  return {
    type: 'EXCELLENT_QUIZ_COMMENTS',
    excellentQuizComments
  }
}

export const getExcellentQuizComments = (assignmentId, quizId, studentId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/excellentQuizComments/students/${studentId}/assignments/${assignmentId}/quizzes/${quizId}`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshExcellentQuizComments(res.body))
      }
    })()
  }
}

export const submitExcellentQuizComment = (taskId, assignmentId, quizId, studentId, content) => {
  const data = {
    assignmentId,
    quizId,
    studentId,
    content
  }
  return dispatch => {
    (async () => {
      const res = await request.post(`../api/v2/excellentQuizComments`, data)
      if (res.status === HTTP_CODE.CREATED) {
        message.success(convertContent('提交成功'))
        dispatch(getExcellentQuizComments(assignmentId, quizId, studentId))
      }
    })()
  }
}
